package com.gmrz.uaf.db;

import com.gmrz.uap.common.db.DataSourceShardingConst;
import com.gmrz.uap.common.db.DataSourceShardingFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class UAFDatasourceConnection
        implements UAFDBConnectionProvider {
    private static final Logger LOG = LogManager.getLogger(UAFDatasourceConnection.class);
    private static final String JNDI_NS_PREFIX = "java:comp/env";
    private static final String DATASOURCE_NAME = "jdbc/fidods";
    private static final String LOG_DATASOURCE_NAME = "jdbc/fidologds";
    private static DataSource dataSource = null;
    private static DataSource logDataSource = null;

    public UAFDatasourceConnection() {
        if (dataSource == null)
            dataSource = getDataSource();
        if (logDataSource == null)
            logDataSource = getLogDataSource();
    }

    public Connection getConnection()
            throws SQLException {
        if (dataSource == null)
            return null;
        Connection conn = dataSource.getConnection();
        return conn;
    }

    public Connection getLogConnection() throws SQLException {
        if (logDataSource == null)
            return null;
        Connection conn = logDataSource.getConnection();
        return conn;
    }

    private DataSource getDataSource() {

        DataSource dataSource = null;

        DataSourceShardingFactory dataSourceShardingFactory;
        try {
            Class clazz = null;
            String value = DataSourceShardingConst.getConfigInfo(DataSourceShardingConst.DATASOURCE_IMPL_CLASS);
            if("com.gmrz.uap.common.db.DataSourceJndiSingleImpl".equals(value)) {
                clazz = Class.forName("com.gmrz.uap.common.db.DataSourceJndiSingleImpl");
            }else if("com.gmrz.uap.common.db.DataSourceJndiMasterSlaveOnlyImpl".equals(value)) {
                clazz = Class.forName("com.gmrz.uap.common.db.DataSourceJndiMasterSlaveOnlyImpl");
            }else if("com.gmrz.uap.common.db.DataSourceJndiShardingDatabaseOnlyImpl".equals(value)) {
                clazz = Class.forName("com.gmrz.uap.common.db.DataSourceJndiShardingDatabaseOnlyImpl");
            }else if("com.gmrz.uap.common.db.DataSourceJndiShardingTableOnlyImpl".equals(value)) {
                clazz = Class.forName("com.gmrz.uap.common.db.DataSourceJndiShardingTableOnlyImpl");
            }else if("com.gmrz.uap.common.db.DataSourceJndiShardingDatabaseAndTableImpl".equals(value)) {
                clazz = Class.forName("com.gmrz.uap.common.db.DataSourceJndiShardingDatabaseAndTableImpl");
            }else if("com.gmrz.uap.common.db.DataSourceJndiShardingAndMasterSlaveImpl".equals(value)) {
                clazz = Class.forName("com.gmrz.uap.common.db.DataSourceJndiShardingAndMasterSlaveImpl");
            }
            dataSourceShardingFactory = (DataSourceShardingFactory) clazz.newInstance();
            dataSource = dataSourceShardingFactory.getInstance();
            return dataSource;

        } catch (Exception e) {
            LOG.error(e);
        }
        return null;
    }

    private DataSource getLogDataSource() {

        DataSource dataSource = null;

        DataSourceShardingFactory dataSourceShardingFactory;
        try {
            Class clazz = null;
            String value = DataSourceShardingConst.getConfigInfo(DataSourceShardingConst.DATASOURCE_LOG_IMPL_CLASS);
            if("com.gmrz.uap.common.db.DataSourceJndiLogsSingleImpl".equals(value)) {
                clazz = Class.forName("com.gmrz.uap.common.db.DataSourceJndiLogsSingleImpl");
            }else if("com.gmrz.uap.common.db.DataSourceJndiLogsShardingDatabaseOnlyImpl".equals(value)){
                clazz = Class.forName("com.gmrz.uap.common.db.DataSourceJndiLogsShardingDatabaseOnlyImpl");
            }
            dataSourceShardingFactory = (DataSourceShardingFactory) clazz.newInstance();
            dataSource = dataSourceShardingFactory.getInstance();
            return dataSource;

        } catch (Exception e) {
            LOG.error(e);
        }
        return null;
    }
}
